package com.jsh.erp.service.depotHead;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.domain.vo.DepotHeadVo4List;
import com.jsh.erp.service.ICommonQuery;
import com.jsh.erp.service.flow.FlowService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.Constants;
import com.jsh.erp.utils.QueryUtils;
import com.jsh.erp.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "depotHead_component")
@DepotHeadResource
public class DepotHeadComponent implements ICommonQuery {

    @Resource
    private DepotHeadService depotHeadService;

    @Resource
    private FlowService flowService;

    @Resource
    private UserService userService;


    @Override
    public Object selectOne(Long id) throws Exception {
        return depotHeadService.getDepotHead(id);
    }

    @Override
    public List<?> select(Map<String, String> map) throws Exception {
        return getDepotHeadList(map);
    }

    //销售订单
    //{"number":"","materialParam":"","type":"其它","subType":"销售订单","organId":"","depotId":"","creator":"","status":"","remark":"",
    // "beginTime":"2024-05-12","endTime":"2024-08-12","createTimeRange":["2024-05-11T16:00:00.000Z","2024-08-11T16:00:00.000Z"]}
    private List<?> getDepotHeadList(Map<String, String> map) throws Exception {
        boolean canSelect = false;
        boolean isXSSH = false;
        String search = map.get(Constants.SEARCH);
        String type = StringUtil.getInfo(search, "type");
        String subType = StringUtil.getInfo(search, "subType");
        String status = StringUtil.getInfo(search, "status");
        if ("销售审核".equals(subType)) {
            isXSSH = true;
//            User user = userService.getCurrentUser();
//            Long roleId = user.getRoleId();
//            Long userId = user.getId();
            subType = "销售订单";
            if (StringUtils.isEmpty(status)) {
                //默认查询待审核中的订单
                status = "9";
            }
            canSelect = true;
//            //销售订单查询 先查流程包含的所有角色或用户，如果当前用户或角色属于流程中的角色或用户，就能查询到待审核订单，否则查不到
//            List<Flow> flowList = flowService.getByName("销售审核");
//            if (!CollectionUtils.isEmpty(flowList)) {
//                for (Flow flow : flowList) {
//                    if (flow.getNodeType() == 1 && Objects.equals(flow.getNodeId(), roleId)) {
//                        //角色
//                        canSelect = true;
//                    }
//                    if (flow.getNodeType() == 2 && Objects.equals(flow.getNodeId(), userId)) {
//                        //用户
//                        canSelect = true;
//                    }
//                }
//            }
        }
        if (isXSSH && !canSelect) {
            //销售审核页面无查询权限
            return new ArrayList<>();
        }

        String hasDebt = StringUtil.getInfo(search, "hasDebt");

        String purchaseStatus = StringUtil.getInfo(search, "purchaseStatus");
        String number = StringUtil.getInfo(search, "number");
        String linkApply = StringUtil.getInfo(search, "linkApply");
        String linkNumber = StringUtil.getInfo(search, "linkNumber");
        String beginTime = StringUtil.getInfo(search, "beginTime");
        String endTime = StringUtil.getInfo(search, "endTime");
        String materialParam = StringUtil.getInfo(search, "materialParam");
        Long organId = StringUtil.parseStrLong(StringUtil.getInfo(search, "organId"));
        Long creator = isXSSH ? null : StringUtil.parseStrLong(StringUtil.getInfo(search, "creator"));
        Long depotId = StringUtil.parseStrLong(StringUtil.getInfo(search, "depotId"));
        Long accountId = StringUtil.parseStrLong(StringUtil.getInfo(search, "accountId"));
        String remark = StringUtil.getInfo(search, "remark");
        List<DepotHeadVo4List> list = depotHeadService.select(type, subType, hasDebt, status, purchaseStatus, number, linkApply, linkNumber,
                beginTime, endTime, materialParam, organId, creator, depotId, accountId, remark, QueryUtils.offset(map), QueryUtils.rows(map), isXSSH);

        return list;
    }

    @Override
    public Long counts(Map<String, String> map) throws Exception {
        String search = map.get(Constants.SEARCH);
        String type = StringUtil.getInfo(search, "type");
        String subType = StringUtil.getInfo(search, "subType");
        String hasDebt = StringUtil.getInfo(search, "hasDebt");
        String status = StringUtil.getInfo(search, "status");
        String purchaseStatus = StringUtil.getInfo(search, "purchaseStatus");
        String number = StringUtil.getInfo(search, "number");
        String linkApply = StringUtil.getInfo(search, "linkApply");
        String linkNumber = StringUtil.getInfo(search, "linkNumber");
        String beginTime = StringUtil.getInfo(search, "beginTime");
        String endTime = StringUtil.getInfo(search, "endTime");
        String materialParam = StringUtil.getInfo(search, "materialParam");
        Long organId = StringUtil.parseStrLong(StringUtil.getInfo(search, "organId"));
        Long creator = StringUtil.parseStrLong(StringUtil.getInfo(search, "creator"));
        Long depotId = StringUtil.parseStrLong(StringUtil.getInfo(search, "depotId"));
        Long accountId = StringUtil.parseStrLong(StringUtil.getInfo(search, "accountId"));
        String remark = StringUtil.getInfo(search, "remark");
        return depotHeadService.countDepotHead(type, subType, hasDebt, status, purchaseStatus, number, linkApply, linkNumber,
                beginTime, endTime, materialParam, organId, creator, depotId, accountId, remark);
    }

    @Override
    public int insert(JSONObject obj, HttpServletRequest request) throws Exception {
        return depotHeadService.insertDepotHead(obj, request);
    }

    @Override
    public int update(JSONObject obj, HttpServletRequest request) throws Exception {
        return depotHeadService.updateDepotHead(obj, request);
    }

    @Override
    public int delete(Long id, HttpServletRequest request) throws Exception {
        return depotHeadService.deleteDepotHead(id, request);
    }

    @Override
    public int deleteBatch(String ids, HttpServletRequest request) throws Exception {
        return depotHeadService.batchDeleteDepotHead(ids, request);
    }

    @Override
    public int checkIsNameExist(Long id, String name) throws Exception {
        return 0;
    }

}
